<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>空闲内存实时监控</title>
    <meta http-equiv=content-type content="text/html; charset=utf-8">
</head>
<body>
<script src="jquery.min.js"></script>
<script src="highcharts.js"></script>
<script src="exporting.js"></script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

<script>
//数据获取
var moniServerIp = prompt("请输入监控的服务器IP：", "");
var moniServerSshUsername = prompt("请输入该服务器的SSH用户名：", "");
var moniServerSshPassword = prompt("请输入该服务器的SSH密码：", "");

var wsServerIP = "127.0.0.1";
var wsServerPort = "19910";
var moniMehtod = "real-time-monitor/memfree";
var moniInterval = 1;
var moniDataUnit = 'MB';

var wsData = 0.0;
wsSend = {
    "action" : moniMehtod,
    "param" : {
        "ip": moniServerIp,
        "ssh_username": moniServerSshUsername,
        "ssh_password": moniServerSshPassword,
        "interval": moniInterval
    }
};

$(function () {
    $(document).ready(function () {
        //----------WebSocket部分--------------
        var ws = new WebSocket("ws://"+wsServerIP+":"+wsServerPort);
        ws.onopen = function() {
            ws.send(JSON.stringify(wsSend));
        };
        ws.onclose = function() {
            console.log('链接已断开');
            ws.close();
        };
        ws.onmessage = function(e) {
            var response = JSON.parse(e.data);
            if(Number(response.errcode) !== 0) {
                console.log(String(response.errmsg));
                ws.close();
                alert('出错啦!' + String(response.errmsg));
                return;
            }
            var divisor = 1;
            switch (moniDataUnit.toLocaleLowerCase()){
                case 'mb':
                    divisor = 1024;
                    break;
            }
            wsData = response.data / divisor;
            console.log("收到服务端的消息：" + wsData);
        };

        //----------highcharts的图标插件部分--------------
        Highcharts.setOptions({
            global: {
                useUTC: false
            }
        });

        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,
                events: {
                    load: function () {
                        // set up the updating of the chart each second
                        var series = this.series[0];
                        setInterval(function () {
                            // current time
                            var x = (new Date()).getTime(),
                                y = wsData * 1.00;
                            series.addPoint([x, y], true, true);
                        }, 1000);
                    }
                }
            },
            title: {
                text: '服务器【' + moniServerIp + '】MemFree指标(单位:' + moniDataUnit + ')'
            },
            xAxis: {
                type: 'datetime',
                tickPixelInterval: 150
            },
            yAxis: {
                title: {
                    text: 'MemFree'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.series.name + '</b><br/>' +
                            Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                            Highcharts.numberFormat(this.y, 2);
                }
            },
            legend: {
                enabled: false
            },
            exporting: {
                enabled: false
            },
            series: [{
                name: 'MemFree指标',
                data: (function () {
                    // generate an array
                    var data = [],
                            time = (new Date()).getTime(),
                            i;

                    for (i = -19; i <= 0; i += 1) {
                        data.push({
                            x: time + i * 1000,
                            y: wsData
                        });
                    }
                    return data;
                }())
            }]
        });
    });
});
</script>
</body>
</html>